package com.buaa.aimall.userModule.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.aimall.Utils.PageReturnResult;
import com.buaa.aimall.userModule.DTO.PageDTO;
import com.buaa.aimall.userModule.Entity.RechargePayOrderEntity;
import com.buaa.aimall.userModule.Mapper.RechargePayOrderMapper;
import org.springframework.stereotype.Service;

@Service
public class RechargePayOrderServiceImpl extends ServiceImpl<RechargePayOrderMapper, RechargePayOrderEntity> implements RechargePayOrderService {

    @Override
    public RechargePayOrderEntity getByMchOrderNo(String mchOrderNo) {
        LambdaQueryWrapper<RechargePayOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargePayOrderEntity::getMchOrderNo, mchOrderNo);
        return this.getOne(queryWrapper);
    }

    @Override
    public PageReturnResult<RechargePayOrderEntity> getPageByUserId(String userId, PageDTO pageDTO) {
        int pageSize = pageDTO.getPageSize();
        int currentPage = pageDTO.getCurrentPage();

        LambdaQueryWrapper<RechargePayOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargePayOrderEntity::getUser, userId);
        queryWrapper.orderByDesc(RechargePayOrderEntity::getCreateTime);

        PageReturnResult<RechargePayOrderEntity> result = new PageReturnResult<>();
        result.setTotalCount(this.count(queryWrapper));

        result.setRecords(this.page(new Page<RechargePayOrderEntity>(currentPage, pageSize), queryWrapper).getRecords());
        return result;
    }
}
